<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>摘要算法、URL编码和base64编 | East'blog</title><meta name="author" content="侯玉东"><meta name="copyright" content="侯玉东"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="摘要算法、URL编码和base64编">
<meta property="og:type" content="article">
<meta property="og:title" content="摘要算法、URL编码和base64编">
<meta property="og:url" content="https://houyudong1989.gitee.io/blog/2024/04/12/python/4%E3%80%81%E6%91%98%E8%A6%81%E7%AE%97%E6%B3%95%E3%80%81URL%E7%BC%96%E7%A0%81%E5%92%8Cbase64%E7%BC%96%E7%A0%81/index.html">
<meta property="og:site_name" content="East&#39;blog">
<meta property="og:description" content="摘要算法、URL编码和base64编">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://houyudong1989.gitee.io/blog/img/library1.jpeg">
<meta property="article:published_time" content="2024-04-12T08:05:35.315Z">
<meta property="article:modified_time" content="2024-04-14T04:12:56.122Z">
<meta property="article:author" content="侯玉东">
<meta property="article:tag" content="摘要算法、URL编码和base64编">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://houyudong1989.gitee.io/blog/img/library1.jpeg"><link rel="shortcut icon" href="/blog/img/head.jpg"><link rel="canonical" href="https://houyudong1989.gitee.io/blog/2024/04/12/python/4%E3%80%81%E6%91%98%E8%A6%81%E7%AE%97%E6%B3%95%E3%80%81URL%E7%BC%96%E7%A0%81%E5%92%8Cbase64%E7%BC%96%E7%A0%81/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/blog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
  root: '/blog/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  infinitegrid: {
    js: 'https://cdn.jsdelivr.net/npm/@egjs/infinitegrid/dist/infinitegrid.min.js',
    buttonText: '加载更多'
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '摘要算法、URL编码和base64编',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2024-04-14 12:12:56'
}</script><script>(win=>{
      win.saveToLocal = {
        set: (key, value, ttl) => {
          if (ttl === 0) return
          const now = Date.now()
          const expiry = now + ttl * 86400000
          const item = {
            value,
            expiry
          }
          localStorage.setItem(key, JSON.stringify(item))
        },
      
        get: key => {
          const itemStr = localStorage.getItem(key)
      
          if (!itemStr) {
            return undefined
          }
          const item = JSON.parse(itemStr)
          const now = Date.now()
      
          if (now > item.expiry) {
            localStorage.removeItem(key)
            return undefined
          }
          return item.value
        }
      }
    
      win.getScript = (url, attr = {}) => new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.src = url
        script.async = true
        script.onerror = reject
        script.onload = script.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          script.onload = script.onreadystatechange = null
          resolve()
        }

        Object.keys(attr).forEach(key => {
          script.setAttribute(key, attr[key])
        })

        document.head.appendChild(script)
      })
    
      win.getCSS = (url, id = false) => new Promise((resolve, reject) => {
        const link = document.createElement('link')
        link.rel = 'stylesheet'
        link.href = url
        if (id) link.id = id
        link.onerror = reject
        link.onload = link.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          link.onload = link.onreadystatechange = null
          resolve()
        }
        document.head.appendChild(link)
      })
    
      win.activateDarkMode = () => {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = () => {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
        if (t === 'dark') activateDarkMode()
        else if (t === 'light') activateLightMode()
      
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
      const detectApple = () => {
        if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
          document.documentElement.classList.add('apple')
        }
      }
      detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><script>(()=>{
  const $loadingBox = document.getElementById('loading-box')
  const $body = document.body
  const preloader = {
    endLoading: () => {
      $body.style.overflow = ''
      $loadingBox.classList.add('loaded')
    },
    initLoading: () => {
      $body.style.overflow = 'hidden'
      $loadingBox.classList.remove('loaded')
    }
  }

  preloader.initLoading()
  window.addEventListener('load',() => { preloader.endLoading() })

  if (true) {
    document.addEventListener('pjax:send', () => { preloader.initLoading() })
    document.addEventListener('pjax:complete', () => { preloader.endLoading() })
  }
})()</script><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/blog/img/head.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">70</div></a><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">26</div></a><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">18</div></a></div><hr class="custom-hr"/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/blog/link/"><i class="fa-fw fas fa-link"></i><span> 友联</span></a></div><div class="menus_item"><a class="site-page" href="/blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg fixed" id="page-header" style="background-image: url('/blog/img/library2.jpeg')"><nav id="nav"><span id="blog-info"><a href="/blog/" title="East'blog"><img class="site-icon" src="/blog/img/head.jpg"/><span class="site-name">East'blog</span></a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/blog/link/"><i class="fa-fw fas fa-link"></i><span> 友联</span></a></div><div class="menus_item"><a class="site-page" href="/blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">摘要算法、URL编码和base64编</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2024-04-12T08:05:35.315Z" title="发表于 2024-04-12 16:05:35">2024-04-12</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2024-04-14T04:12:56.122Z" title="更新于 2024-04-14 12:12:56">2024-04-14</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/blog/categories/python-%E7%88%AC%E8%99%AB/">python 爬虫</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="摘要算法、URL编码和base64编"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="摘要算法"><a href="#摘要算法" class="headerlink" title="摘要算法"></a>摘要算法</h1><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">摘要算法（Hash Algorithm）是一种将任意长度的数据映射为固定长度摘要（哈希值）的算法。 </span><br><span class="line">摘要算法在数据完整性验证、密码学、数字签名等领域有着广泛的应用。然而，在使用摘要算法时需要注意选择合适的算法和适当的安全性措施，以防止哈希碰撞等安全问题的发生。</span><br></pre></td></tr></table></figure>

<h2 id="特点"><a href="#特点" class="headerlink" title="特点"></a>特点</h2><ul>
<li>固定长度输出</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">摘要算法将任意长度的输入数据映射为固定长度的输出，通常以十六进制或其他方式表示。</span><br></pre></td></tr></table></figure>

<ul>
<li>唯一性</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">对于给定的输入，摘要算法应该生成唯一的输出。虽然在理论上可能存在不同的输入产生相同的输出（哈希碰撞），但是好的摘要算法应该在实践中避免这种情况发生。</span><br></pre></td></tr></table></figure>

<ul>
<li>不可逆性</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">摘要算法是单向的，即从摘要值无法还原出原始数据。这意味着无法通过摘要值来获取原始数据的内容。</span><br></pre></td></tr></table></figure>

<ul>
<li>快速性</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">摘要算法的计算速度应该足够快，以便在实际应用中进行高效的处理。</span><br></pre></td></tr></table></figure>

<h2 id="常见摘要算法类型"><a href="#常见摘要算法类型" class="headerlink" title="常见摘要算法类型"></a>常见摘要算法类型</h2><ul>
<li>MD5</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">MD5（Message Digest Algorithm <span class="number">5</span>）：一种常用的哈希函数，生成 <span class="number">128</span> 位（<span class="number">16</span> 字节）的哈希值。然而，由于其安全性问题，现在已经不推荐用于安全敏感的应用。</span><br></pre></td></tr></table></figure>

<ul>
<li>SHA</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">SHA（Secure Hash Algorithm）：SHA 系列包括了多种摘要算法，如 SHA-<span class="number">1</span>、SHA-<span class="number">256</span>、SHA-<span class="number">512</span> 等。SHA-<span class="number">256</span> 和 SHA-<span class="number">512</span> 是比较常用的安全哈希算法，分别生成 <span class="number">256</span> 位和 <span class="number">512</span> 位的哈希值，被广泛用于密码学和数据完整性验证等领域。</span><br></pre></td></tr></table></figure>

<h2 id="主要用途"><a href="#主要用途" class="headerlink" title="主要用途"></a>主要用途</h2><ul>
<li>数据完整性验证</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">摘要算法可以用于验证数据在传输或存储过程中是否发生了改变。通过计算原始数据的摘要值，并将其与接收到的数据的摘要值进行比较，可以检测到数据是否被篡改或损坏。</span><br></pre></td></tr></table></figure>

<ul>
<li>密码学应用</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">摘要算法在密码学中扮演重要角色，用于生成密码的哈希值、数字签名、消息认证码（MAC）等。密码哈希函数通常用于存储密码的安全散列，以避免在数据库中存储明文密码。</span><br></pre></td></tr></table></figure>

<ul>
<li>数据唯一性标识</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">摘要算法可以将任意长度的数据映射为固定长度的唯一标识符。这在数据去重、索引和标识等方面具有重要意义。</span><br></pre></td></tr></table></figure>

<ul>
<li>安全性应用</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">摘要算法在数字证书、SSL/TLS、数字签名等安全协议中被广泛应用，用于验证数据的完整性和真实性，以及防止伪造和篡改。</span><br></pre></td></tr></table></figure>

<ul>
<li>消息认证</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">摘要算法可以用于生成消息认证码（MAC），用于验证消息的完整性和真实性，确保消息未被篡改或伪造。</span><br></pre></td></tr></table></figure>

<h2 id="MD5实现"><a href="#MD5实现" class="headerlink" title="MD5实现"></a>MD5实现</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> hashlib <span class="keyword">import</span> md5</span><br><span class="line"></span><br><span class="line">result = md5()</span><br><span class="line">result.update(<span class="string">&quot;hello&quot;</span>.encode(<span class="string">&quot;utf-8&quot;</span>))</span><br><span class="line">result.update(<span class="string">&quot;east&quot;</span>.encode(<span class="string">&quot;utf-8&quot;</span>)) <span class="comment">#可以添加多个被加密的内容</span></span><br><span class="line">mymd5 = result.hexdigest()</span><br><span class="line"><span class="built_in">print</span>(mymd5)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 我们把密文丢到网页里. 发现有些网站可以直接解密. 但其实不然. 这里并不是直接解密MD5. 而是&quot;撞库&quot;.  </span></span><br><span class="line"><span class="comment"># 就是它网站里存储了大量的MD5的值. 为防止撞库，我们可以在加密时添加自定义内容，防止破译</span></span><br><span class="line">salt = <span class="string">&quot;加麻加辣&quot;</span></span><br><span class="line">result1 = md5(salt.encode(<span class="string">&quot;utf-8&quot;</span>))  <span class="comment"># 加盐</span></span><br><span class="line">result1.update(<span class="string">&quot;east&quot;</span>.encode(<span class="string">&quot;utf-8&quot;</span>))</span><br><span class="line">mymd5 = result1.hexdigest()</span><br><span class="line"><span class="built_in">print</span>(mymd5)</span><br></pre></td></tr></table></figure>

<h2 id="SHA实现"><a href="#SHA实现" class="headerlink" title="SHA实现"></a>SHA实现</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">    <span class="keyword">from</span> hashlib <span class="keyword">import</span> sha1, sha256</span><br><span class="line">    <span class="comment"># sha1value = sha1()</span></span><br><span class="line">    sha1value = sha1(<span class="string">b&#x27;salt&#x27;</span>) <span class="comment">#增加自定义内容，提升破译难度</span></span><br><span class="line">    sha1value.update(<span class="string">b&#x27;east&#x27;</span>)</span><br><span class="line">    <span class="built_in">print</span>(sha1value.hexdigest())</span><br><span class="line"></span><br><span class="line">    <span class="comment"># sha256value = sha256()</span></span><br><span class="line">    sha256value = sha256(<span class="string">b&#x27;salt&#x27;</span>) <span class="comment">#增加自定义内容，提升破译难度</span></span><br><span class="line">    sha256value.update(<span class="string">b&#x27;east&#x27;</span>)</span><br><span class="line">    <span class="built_in">print</span>(sha256value.hexdigest())</span><br><span class="line"><span class="comment">#当使用哈希函数计算摘要时，通常需要将输入数据转换为字节字符串的形式，因为哈希函数操作的是字节数据而不是字符串。</span></span><br><span class="line"><span class="comment">#前缀b表示一个字节字符串，也可以使用上面编码方式处理，比如：前缀b表示一个字节字符串</span></span><br></pre></td></tr></table></figure>

<h1 id="URL编码"><a href="#URL编码" class="headerlink" title="URL编码"></a>URL编码</h1><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">URL 编解码是指将 URL 中的特殊字符进行编码或解码，以确保它们在传输过程中的正确性和安全性。URL 编码也称为百分号编码（percent-encoding），使用百分号加上两位十六进制数来表示非 ASCII 字符。在编码中，所有非字母数字字符都被替换为百分号（%），后跟两个表示该字符 ASCII 值的十六进制数。这样可以确保 URL 中的特殊字符不会与 URL 结构中的字符混淆，从而防止出现歧义或错误。</span><br></pre></td></tr></table></figure>

<h2 id="字符串编解码"><a href="#字符串编解码" class="headerlink" title="字符串编解码"></a>字符串编解码</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># URL 字符串编码</span></span><br><span class="line">  url = <span class="string">&quot;土豆丝&quot;</span></span><br><span class="line">  encoded_url = urllib.parse.quote(url)</span><br><span class="line">  <span class="built_in">print</span>(<span class="string">&quot;Encoded URL:&quot;</span>, encoded_url)</span><br><span class="line">  <span class="comment"># URL  字符串解码</span></span><br><span class="line">  decoded_url = urllib.parse.unquote(encoded_url)</span><br><span class="line">  <span class="built_in">print</span>(<span class="string">&quot;Decoded URL:&quot;</span>, decoded_url)</span><br></pre></td></tr></table></figure>

<h2 id="字典编解码"><a href="#字典编解码" class="headerlink" title="字典编解码"></a>字典编解码</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># URL 字典编码</span></span><br><span class="line">data = &#123;</span><br><span class="line">    <span class="string">&quot;keyword&quot;</span>:<span class="string">&quot;土豆丝&quot;</span>,</span><br><span class="line">    <span class="string">&quot;cat&quot;</span>:<span class="number">1001</span></span><br><span class="line">&#125;</span><br><span class="line">encoded_data = urllib.parse.urlencode(data)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;Encoded URL:&quot;</span>, encoded_data)</span><br><span class="line"><span class="comment"># URL 字典解码</span></span><br><span class="line">decoded_data = urllib.parse.unquote(encoded_data)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;Decoded URL:&quot;</span>, decoded_data)</span><br></pre></td></tr></table></figure>

<h1 id="base64编码"><a href="#base64编码" class="headerlink" title="base64编码"></a>base64编码</h1><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Base64 编码是一种用于将二进制数据转换为可打印字符的编码方式，它基于 64 个可打印的ASCII字符，包括字母（A-Z、a-z）、数字（0-9）和两个额外的符号（+ 和 /），因此得名 Base64。</span><br><span class="line">Base64 编码的原理是将每 3 个字节（24 位）的数据编码成 4 个 Base64 字符，每个 Base64 字符对应 6 个比特位。如果输入数据的长度不是 3 的倍数，就需要在末尾添加适当数量的填充字符（=）来使其长度能够被 3 整除。</span><br></pre></td></tr></table></figure>

<h2 id="为何要使用base64编码"><a href="#为何要使用base64编码" class="headerlink" title="为何要使用base64编码"></a>为何要使用base64编码</h2><h3 id="base64优点"><a href="#base64优点" class="headerlink" title="base64优点"></a>base64优点</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">- 算法是编码，不是压缩，编码后只会增加字节数（一般是比之前的多1/3，比如之前是3， 编码后是4）</span><br><span class="line">- 算法简单，基本不影响效率</span><br><span class="line">- 算法可逆，解码很方便，不用于私密传输。</span><br><span class="line">- 通过编码肉眼不能直接读出原始内容。</span><br><span class="line">- 加密后的字符串只有【0-9a-zA-Z+/=】 ，不可打印字符（转译字符）也可以传输（关键！！！）</span><br></pre></td></tr></table></figure>

<h3 id="使用base64原因"><a href="#使用base64原因" class="headerlink" title="使用base64原因"></a>使用base64原因</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">有些网络传输协议是为了传输`ASCII文本`设计的，当你使用其传输二进制流时（比如视频/图片），二进制流中的数据可能会被协议错误的识别为控制字符等等，因而出现错误。那这时就要将二进制流传输编码，因为有些8Bit字节码并没有对应的ASCII字符。</span><br><span class="line"></span><br><span class="line">比如十进制ASCII码8对应的是后退符号(backspace), 如果被编码的数据中包含这个数值，那么编码出来的结果在很多编程语言里会导致前一个字符被删掉。又比如ASCII码0对应的是空字符，在一些编程语言里代表字符串结束，后续的数据就不会被处理了。</span><br><span class="line"></span><br><span class="line">用Base64编码因为限定了用于编码的字符集，确保编码的结果可打印且无歧义。</span><br><span class="line"></span><br><span class="line">不同的网络节点设备交互数据需要：设备A把base64编码后的数据封装在`json字符串`里，设备B先解析json拿到value，再进行base64解码拿到想要的数据。</span><br></pre></td></tr></table></figure>

<ul>
<li>早年制定的一些协议都是只支持文本设定的。随着不断发展需要支持非文本了，才搞了一个base64做兼容</li>
<li>虽然编码之后的数据与加密一样都具有不可见性，但编码与加密的概念并不一样。编码是公开的，任何人都可以解码；而加密则相反，你只希望自己或者特定的人才可以对内容进行解密。</li>
</ul>
<h2 id="base64码表"><a href="#base64码表" class="headerlink" title="base64码表"></a>base64码表</h2><p><img src="1713066796527.png" alt="1713066796527"></p>
<ul>
<li>一个Base64字符实际上代表着6个二进制位(bit)，4个Base64字符对应3字节字符串&#x2F;二进制数据。</li>
</ul>
<h2 id="3个字符为一组的base64编码方式"><a href="#3个字符为一组的base64编码方式" class="headerlink" title="3个字符为一组的base64编码方式"></a>3个字符为一组的base64编码方式</h2><p><img src="1713067133362.png" alt="1713067133362"></p>
<h2 id="不足三个字符为一组的base64编码方式"><a href="#不足三个字符为一组的base64编码方式" class="headerlink" title="不足三个字符为一组的base64编码方式"></a>不足三个字符为一组的base64编码方式</h2><p><img src="1713067055781.png" alt="1713067055781"></p>
<h2 id="base编码流程"><a href="#base编码流程" class="headerlink" title="base编码流程"></a>base编码流程</h2><p><img src="1713067176646.png" alt="1713067176646"></p>
<h2 id="变种base64"><a href="#变种base64" class="headerlink" title="变种base64"></a>变种base64</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&quot;变种 Base64&quot; 可能指的是对 Base64 编码进行一些修改或自定义以满足特定需求的编码方式。这种变种可能是针对特定的应用场景或特定的数据类型而进行的定制。</span><br><span class="line"></span><br><span class="line">以下是一些可能的 Base64 变种：</span><br><span class="line"></span><br><span class="line">自定义字符集：有时候，为了适应特定的需求或环境，可能会自定义 Base64 字符集，比如使用不同的字符、添加额外的字符或符号，或者排除某些字符。</span><br><span class="line"></span><br><span class="line">填充字符的改变：虽然 Base64 规范中指定使用 = 作为填充字符，但是在某些变种中可能会选择不同的填充字符，或者干脆省略填充字符。</span><br><span class="line"></span><br><span class="line">定制化编码表：Base64 编码表规定了字符与编码值的对应关系，有些变种可能会调整这种对应关系，甚至采用完全不同的编码表。</span><br><span class="line"></span><br><span class="line">特定用途的变种：有时针对特定的数据类型或应用场景，可能会对 Base64 进行一些改进或优化，以提高效率或增强安全性。</span><br><span class="line"></span><br><span class="line">这些变种通常都是基于 Base64 的基本原理和算法进行的修改，但是需要注意的是，由于 Base64 是一种广泛使用的标准编码方式，在实际应用中，如果使用了变种的 Base64 编码，接收方必须也能理解并正确解码这种变种才能正常工作。</span><br></pre></td></tr></table></figure>

<h3 id="最常见的base64变种"><a href="#最常见的base64变种" class="headerlink" title="最常见的base64变种"></a>最常见的base64变种</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">最常见的base64变种是将+替换成-，将/替换成_</span><br><span class="line">在解码的时候需要先进行文本替换，在进行解码</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 方式1</span></span><br><span class="line">data = res.text.replace(<span class="string">&quot;-&quot;</span>, <span class="string">&quot;+&quot;</span>).replace(<span class="string">&quot;_&quot;</span>, <span class="string">&quot;/&quot;</span>)</span><br><span class="line">base64.b64decode(data)</span><br><span class="line"><span class="comment"># 方式2</span></span><br><span class="line">data = base64.b64decode(res.text, altchars=<span class="string">b&quot;-_&quot;</span>)  <span class="comment"># base64解码成字节流</span></span><br></pre></td></tr></table></figure>

<h2 id="示例"><a href="#示例" class="headerlink" title="示例"></a>示例</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">    <span class="keyword">import</span> base64</span><br><span class="line"><span class="comment"># -----------------编码----------------------</span></span><br><span class="line">    bs = <span class="string">&quot;you&quot;</span>.encode(<span class="string">&quot;utf-8&quot;</span>)</span><br><span class="line">    <span class="comment"># 把字节转化成b64</span></span><br><span class="line">    <span class="built_in">print</span>(base64.b64encode(bs).decode())</span><br><span class="line"></span><br><span class="line">    bs = <span class="string">&quot;yo&quot;</span>.encode(<span class="string">&quot;utf-8&quot;</span>)</span><br><span class="line">    <span class="comment"># 把字节转化成b64</span></span><br><span class="line">    <span class="built_in">print</span>(base64.b64encode(bs).decode())</span><br><span class="line"></span><br><span class="line">    bs = <span class="string">&quot;y&quot;</span>.encode(<span class="string">&quot;utf-8&quot;</span>)</span><br><span class="line">    <span class="comment"># 把字节转化成b64</span></span><br><span class="line">    <span class="built_in">print</span>(base64.b64encode(bs).decode())</span><br><span class="line"><span class="comment"># -----------------解码----------------------</span></span><br><span class="line">    s = <span class="string">&quot;eW91&quot;</span></span><br><span class="line">    ret = base64.b64decode(s)</span><br><span class="line">    <span class="built_in">print</span>(ret)</span><br><span class="line"></span><br><span class="line">    s = <span class="string">&quot;eW91eQ==&quot;</span></span><br><span class="line">    ret = base64.b64decode(s)</span><br><span class="line">    <span class="built_in">print</span>(ret)</span><br><span class="line"></span><br><span class="line">    s = <span class="string">&quot;eQ==&quot;</span></span><br><span class="line">    ret = base64.b64decode(s)</span><br><span class="line">    <span class="built_in">print</span>(ret)</span><br></pre></td></tr></table></figure></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>文章作者: </span><span class="post-copyright-info"><a href="https://houyudong1989.gitee.io/blog">侯玉东</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>文章链接: </span><span class="post-copyright-info"><a href="https://houyudong1989.gitee.io/blog/2024/04/12/python/4%E3%80%81%E6%91%98%E8%A6%81%E7%AE%97%E6%B3%95%E3%80%81URL%E7%BC%96%E7%A0%81%E5%92%8Cbase64%E7%BC%96%E7%A0%81/">https://houyudong1989.gitee.io/blog/2024/04/12/python/4%E3%80%81%E6%91%98%E8%A6%81%E7%AE%97%E6%B3%95%E3%80%81URL%E7%BC%96%E7%A0%81%E5%92%8Cbase64%E7%BC%96%E7%A0%81/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://houyudong1989.gitee.io/blog" target="_blank">East'blog</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/blog/tags/%E6%91%98%E8%A6%81%E7%AE%97%E6%B3%95%E3%80%81URL%E7%BC%96%E7%A0%81%E5%92%8Cbase64%E7%BC%96/">摘要算法、URL编码和base64编</a></div><div class="post_share"><div class="social-share" data-image="/blog/img/library1.jpeg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/blog/2024/04/12/python/5%E3%80%81%E5%8A%A0%E5%AF%86%E7%AE%97%E6%B3%95/" title="加密算法"><img class="cover" src="/blog/img/library1.jpeg" onerror="onerror=null;src='/blog/img/friend_404.gif'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">加密算法</div></div></a></div><div class="next-post pull-right"><a href="/blog/2024/04/12/python/3%E3%80%81xpath%E4%BD%BF%E7%94%A8/" title="xpath使用"><img class="cover" src="/blog/img/library1.jpeg" onerror="onerror=null;src='/blog/img/friend_404.gif'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">xpath使用</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/blog/img/head.jpg" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">侯玉东</div><div class="author-info__description">生命不息，奋斗不止</div></div><div class="card-info-data site-data is-center"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">70</div></a><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">26</div></a><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">18</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://gitee.com/houyudong1989"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://gitee.com/houyudong1989" target="_blank" title="Github"><i class="fab fa-github" style="color: #24292e;"></i></a><a class="social-icon" href="mailto:420098321@qq.com" target="_blank" title="Email"><i class="fas fa-envelope" style="color: #4a7dbe;"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">欢迎来到我的博客！！！</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%91%98%E8%A6%81%E7%AE%97%E6%B3%95"><span class="toc-number">1.</span> <span class="toc-text">摘要算法</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%89%B9%E7%82%B9"><span class="toc-number">1.1.</span> <span class="toc-text">特点</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%B8%B8%E8%A7%81%E6%91%98%E8%A6%81%E7%AE%97%E6%B3%95%E7%B1%BB%E5%9E%8B"><span class="toc-number">1.2.</span> <span class="toc-text">常见摘要算法类型</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%BB%E8%A6%81%E7%94%A8%E9%80%94"><span class="toc-number">1.3.</span> <span class="toc-text">主要用途</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#MD5%E5%AE%9E%E7%8E%B0"><span class="toc-number">1.4.</span> <span class="toc-text">MD5实现</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#SHA%E5%AE%9E%E7%8E%B0"><span class="toc-number">1.5.</span> <span class="toc-text">SHA实现</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#URL%E7%BC%96%E7%A0%81"><span class="toc-number">2.</span> <span class="toc-text">URL编码</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%BC%96%E8%A7%A3%E7%A0%81"><span class="toc-number">2.1.</span> <span class="toc-text">字符串编解码</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AD%97%E5%85%B8%E7%BC%96%E8%A7%A3%E7%A0%81"><span class="toc-number">2.2.</span> <span class="toc-text">字典编解码</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#base64%E7%BC%96%E7%A0%81"><span class="toc-number">3.</span> <span class="toc-text">base64编码</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%BA%E4%BD%95%E8%A6%81%E4%BD%BF%E7%94%A8base64%E7%BC%96%E7%A0%81"><span class="toc-number">3.1.</span> <span class="toc-text">为何要使用base64编码</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#base64%E4%BC%98%E7%82%B9"><span class="toc-number">3.1.1.</span> <span class="toc-text">base64优点</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8base64%E5%8E%9F%E5%9B%A0"><span class="toc-number">3.1.2.</span> <span class="toc-text">使用base64原因</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#base64%E7%A0%81%E8%A1%A8"><span class="toc-number">3.2.</span> <span class="toc-text">base64码表</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3%E4%B8%AA%E5%AD%97%E7%AC%A6%E4%B8%BA%E4%B8%80%E7%BB%84%E7%9A%84base64%E7%BC%96%E7%A0%81%E6%96%B9%E5%BC%8F"><span class="toc-number">3.3.</span> <span class="toc-text">3个字符为一组的base64编码方式</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%8D%E8%B6%B3%E4%B8%89%E4%B8%AA%E5%AD%97%E7%AC%A6%E4%B8%BA%E4%B8%80%E7%BB%84%E7%9A%84base64%E7%BC%96%E7%A0%81%E6%96%B9%E5%BC%8F"><span class="toc-number">3.4.</span> <span class="toc-text">不足三个字符为一组的base64编码方式</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#base%E7%BC%96%E7%A0%81%E6%B5%81%E7%A8%8B"><span class="toc-number">3.5.</span> <span class="toc-text">base编码流程</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%98%E7%A7%8Dbase64"><span class="toc-number">3.6.</span> <span class="toc-text">变种base64</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9C%80%E5%B8%B8%E8%A7%81%E7%9A%84base64%E5%8F%98%E7%A7%8D"><span class="toc-number">3.6.1.</span> <span class="toc-text">最常见的base64变种</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%A4%BA%E4%BE%8B"><span class="toc-number">3.7.</span> <span class="toc-text">示例</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/blog/2024/05/06/c++/5%E3%80%81%E5%AD%97%E7%AC%A6%E5%92%8C%E5%AD%97%E7%AC%A6%E4%B8%B2/" title="字符和字符串"><img src="/blog/img/library1.jpeg" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="字符和字符串"/></a><div class="content"><a class="title" href="/blog/2024/05/06/c++/5%E3%80%81%E5%AD%97%E7%AC%A6%E5%92%8C%E5%AD%97%E7%AC%A6%E4%B8%B2/" title="字符和字符串">字符和字符串</a><time datetime="2024-05-06T11:15:27.280Z" title="发表于 2024-05-06 19:15:27">2024-05-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2024/05/06/c++/4%E3%80%81%E6%8C%87%E9%92%88%E5%92%8C%E5%87%BD%E6%95%B0%E6%8C%87%E9%92%88/" title="指针和函数指针"><img src="/blog/img/library1.jpeg" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="指针和函数指针"/></a><div class="content"><a class="title" href="/blog/2024/05/06/c++/4%E3%80%81%E6%8C%87%E9%92%88%E5%92%8C%E5%87%BD%E6%95%B0%E6%8C%87%E9%92%88/" title="指针和函数指针">指针和函数指针</a><time datetime="2024-05-06T11:14:29.047Z" title="发表于 2024-05-06 19:14:29">2024-05-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2024/05/05/python/7%E3%80%81m3u8%E8%A7%86%E9%A2%91%E6%A0%BC%E5%BC%8F/" title="m3u8格式"><img src="/blog/img/library1.jpeg" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="m3u8格式"/></a><div class="content"><a class="title" href="/blog/2024/05/05/python/7%E3%80%81m3u8%E8%A7%86%E9%A2%91%E6%A0%BC%E5%BC%8F/" title="m3u8格式">m3u8格式</a><time datetime="2024-05-05T01:43:26.865Z" title="发表于 2024-05-05 09:43:26">2024-05-05</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2024/04/30/android/charles%E5%AE%89%E5%8D%93%E6%89%8B%E6%9C%BA%E6%8A%93%E5%8C%85/" title="charles 抓包"><img src="/blog/img/library1.jpeg" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="charles 抓包"/></a><div class="content"><a class="title" href="/blog/2024/04/30/android/charles%E5%AE%89%E5%8D%93%E6%89%8B%E6%9C%BA%E6%8A%93%E5%8C%85/" title="charles 抓包">charles 抓包</a><time datetime="2024-04-30T06:49:29.279Z" title="发表于 2024-04-30 14:49:29">2024-04-30</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2024/04/26/python/13%E3%80%81playwright/" title="playwright"><img src="/blog/img/library1.jpeg" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="playwright"/></a><div class="content"><a class="title" href="/blog/2024/04/26/python/13%E3%80%81playwright/" title="playwright">playwright</a><time datetime="2024-04-26T07:30:36.069Z" title="发表于 2024-04-26 15:30:36">2024-04-26</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2024 By 侯玉东</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/blog/js/utils.js"></script><script src="/blog/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.umd.min.js"></script><div class="js-pjax"></div><div class="aplayer no-destroy" data-id="8873950678" data-server="netease" data-type="playlist" data-fixed="true" data-autoplay="true"> </div><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="false"></script><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-fluttering-ribbon.min.js"></script><script id="click-heart" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/click-heart.min.js" async="async" mobile="false"></script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/metingjs/dist/Meting.min.js"></script><script src="https://cdn.jsdelivr.net/npm/pjax/pjax.min.js"></script><script>let pjaxSelectors = ["head > title","#config-diff","#body-wrap","#rightside-config-hide","#rightside-config-show",".js-pjax"]

var pjax = new Pjax({
  elements: 'a:not([target="_blank"])',
  selectors: pjaxSelectors,
  cacheBust: false,
  analytics: false,
  scrollRestoration: false
})

document.addEventListener('pjax:send', function () {

  // removeEventListener
  btf.removeGlobalFnEvent('pjax')
  btf.removeGlobalFnEvent('themeChange')

  document.getElementById('rightside').classList.remove('rightside-show')
  
  if (window.aplayers) {
    for (let i = 0; i < window.aplayers.length; i++) {
      if (!window.aplayers[i].options.fixed) {
        window.aplayers[i].destroy()
      }
    }
  }

  typeof typed === 'object' && typed.destroy()

  //reset readmode
  const $bodyClassList = document.body.classList
  $bodyClassList.contains('read-mode') && $bodyClassList.remove('read-mode')

  typeof disqusjs === 'object' && disqusjs.destroy()
})

document.addEventListener('pjax:complete', function () {
  window.refreshFn()

  document.querySelectorAll('script[data-pjax]').forEach(item => {
    const newScript = document.createElement('script')
    const content = item.text || item.textContent || item.innerHTML || ""
    Array.from(item.attributes).forEach(attr => newScript.setAttribute(attr.name, attr.value))
    newScript.appendChild(document.createTextNode(content))
    item.parentNode.replaceChild(newScript, item)
  })

  GLOBAL_CONFIG.islazyload && window.lazyLoadInstance.update()

  typeof panguInit === 'function' && panguInit()

  // google analytics
  typeof gtag === 'function' && gtag('config', '', {'page_path': window.location.pathname});

  // baidu analytics
  typeof _hmt === 'object' && _hmt.push(['_trackPageview',window.location.pathname]);

  typeof loadMeting === 'function' && document.getElementsByClassName('aplayer').length && loadMeting()

  // prismjs
  typeof Prism === 'object' && Prism.highlightAll()
})

document.addEventListener('pjax:error', e => {
  if (e.request.status === 404) {
    pjax.loadUrl('/blog/404.html')
  }
})</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>